package com.honey.mybatis.generator.entity.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.ElseBlock;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.JavaBeansUtil;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;

public class SetterMethod extends AbstractMethod {

	private Column column = null;
	
	private String fieldName = null;
	
	private FullyQualifiedJavaType fieldType = null;
	
	public SetterMethod(
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(null, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	public SetterMethod( AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	@Override
	protected String getMethodName() {
		return JavaBeansUtil.getSetterMethodName(
			super.getMethodName(column)
		);
	}
	
	@Override
	protected Parameter[] getParameter() {
		return new Parameter[]{
				new Parameter( fieldType , fieldName),
		};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return new String[]{
			column.getComment(),	
		};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		MethodDecoration answer = super.getMethodDecoration(); 
		answer.setFinal(mybatisPlugin.isEntityFinalMethod());
		return answer;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#isBaseType()
	 */
	@Override
	protected boolean isBaseType() {
		return mybatisPlugin.getProjectEnvironmentHolder().isForcePrimitiveType();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		if(mybatisPlugin.isEntityTrimStrings() && column.isStringColumn() && column.getEnumSchema() == null){
			IfBlock ifBlock = new IfBlock();
			ifBlock.setCondition( (new StringBuilder()).append(fieldName).append(" != null" ).toString());
			ifBlock.addBodyLine((new StringBuilder()).append(JavaKeyWord.OPERATION_THIS).append(fieldName).append(JavaKeyWord.OPERATION_BRACKET_EQUALS).append(fieldName).append(".trim()").toString());
			ElseBlock elseBlock =new ElseBlock();
			elseBlock.addBodyLine(new StringBuilder().append(JavaKeyWord.OPERATION_THIS).append(fieldName).append(JavaKeyWord.OPERATION_BRACKET_EQUALS).append(JavaKeyWord.NULL).toString());
			method.addBlocks(ifBlock);
			method.addBlocks(elseBlock);
		}else{
			method.addBodyLine(new StringBuilder().append(JavaKeyWord.OPERATION_THIS).append(fieldName).append(JavaKeyWord.OPERATION_BRACKET_EQUALS).append(fieldName).toString());
		}
		if(mybatisPlugin.isEntityReturnThis()){
			method.setReturnType(clazz.getType());
			method.addBodyLine("return this");
		}
	}

	public void setColumn(Column column) {
		this.column = column;
		fieldName = getFieldName(column);
		fieldType = getFieldType(introspectSchema.getSchema(),column);
	}
	
	
}
